<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>添加供应商商品管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/layui/css/layui.css" />
<link rel="stylesheet" href="/static/ui/css/ui.css" />
</head>

<body>
    <form class="layui-form" action="" lay-filter="editform">
        <div class="mainBox">
            <div class="main-container">
                <div class="main-container">
                    <div class="layui-form-item">
                        <label class="layui-form-label">名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" lay-verify="required|len" autocomplete="off" min="1"
                                placeholder="请输入商品名称" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">商品价格</label>
                        <div class="layui-input-block">
                            <input type="number" name="price" lay-verify="required|len" autocomplete="off" min="1"
                                placeholder="请输入商品价格" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">展示价格</label>
                        <div class="layui-input-block">
                            <input type="number" name="price_show" autocomplete="off" min="1"
                                placeholder="请输入展示价格" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">商品型号</label>
                        <div class="layui-input-block">
                            <input type="text" name="model" autocomplete="off" min="1"
                                placeholder="请输入商品型号" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">选择供应商</label>
                        <div class="layui-input-block">
                            <button type="button" class="pear-btn pear-btn-sm" id="supplier_choose">
                                <i class="layui-icon layui-icon-search"></i>
                                <span id="supplier_name_txt">选择供应商</span>
                            </button>
                            <input type="hidden" name="supplier_id" id="supplier_id" value="0">
                            <input type="hidden" name="supplier_name" id="supplier_name" value="0">
                        </div>
                    </div>
                    <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label">备注</label>
                        <div class="layui-input-block">
                            <textarea name="remark" placeholder="请输入内容" class="layui-textarea"></textarea>
                        </div>
                    </div>

                </div>
            </div>
        </div>
        <div class="bottom">
            <div class="button-container">
                <input type="hidden" name="id" />
                <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                    lay-filter="supplier_goods-save">
                    <i class="layui-icon layui-icon-ok"></i>
                    提交
                </button>
            </div>
        </div>
    </form>
    <script src="../config.js"></script>
    <script src="/static/layui/layui.js"></script>
    <script src="/static/ui/ui.js"></script>
    <script>
        layui.use(['form', 'layer', 'tree'], function () {
            let form = layui.form
                , layer = layui.layer
                , $ = layui.$
                , req = _req();
            form.verify({
                len: (val, item) => {
                    let min = item.getAttribute("min")
                    if (val.length < min) {
                        return '长度不能小于' + min;
                    }
                }
            })
            if (req.id) {
                //渲染
                _get(layui, 'supplier_goods/editBefore?id=' + req.id, res => {
                    form.val('editform', res)
                });
            }
            $('#supplier_choose').click(() => {
                layer.open({
                    type: 2,
                    title: '修改',
                    shade: 0.1,
                    offset: 'rt',
                    area: ['80%', '100%'],
                    anim: 1,
                    maxmin: true,
                    content: './choose.html'
                });
            })
            window.setVal = function(val, name) {
                $('#supplier_id').val(val)
                $("#supplier_name_txt").text(name)
                $("#supplier_name").val(name)
            }
            //监听提交
            form.on('submit(supplier_goods-save)', function (data) {
                data = data.field;
                if(data.supplier_id*1 < 1) {
                    layer.msg('请选择供应商')
                    return false;
                }
                let postUrl;
                if (req.id) {
                    postUrl = 'supplier_goods/edit';
                } else {
                    postUrl = 'supplier_goods/add';
                }
                _post(layui, postUrl, data, res => {
                    //console.log(res)
                    parent.layui.table.reload("supplier_goods-table");
                    parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页

                })

                return false;
            });

        });
    </script>

</body>

</html>